import pymysql
import base64
# 连接数据库
conn = pymysql.connect(host='localhost',
                       user='root',
                       password='root',
                       db='md',
                       charset='utf8')
# 创建一个游标
cursor = conn.cursor()
插入数据
数据直接写在sql后面
sql = "insert into user(img) values ('%s') " % '123'

with open('../static/sina.png', 'rb') as f:  # 以二进制读取图片
    data = f.read()
    encodestr = base64.b64encode(data)  # 得到 byte 编码的数据

    sql = "insert into user(img) values ('%s') " % str(encodestr, 'utf-8')

    cursor.execute(sql)
    conn.commit()  # 提交，不然无法保存插入或者修改的数据(这个一定不要忘记加上)
    cursor.close()  # 关闭游标
    conn.close()

sql = "select img from user where id = 15"
cursor.execute(sql)
res = cursor.fetchone()

print(res[0])
